#include "c_gamecenter_manager.h"

#include "../sleipner_config.h"

#ifdef _PLATFORM_IOS
#include "GameCenter_c.h"
#endif


static void OnSubmitCB(const char* ID);
static void OnErrorCB(const char* ID);

static void LogInOnSuccessCB(const wchar_t* PlayerName);
static void LogInOnErrorCB();

CGameCenterManager*		CGameCenterManager::m_pInst = NULL;

CGameCenterManager*		CGameCenterManager::Instance()
{
	return m_pInst;
}

CGameCenterManager::CGameCenterManager() 
{
    for(int i = 0; (int)i < MAX_ACHIEVEMENT; i++)
	{
		m_IsCompelete[i] = false;
		m_CurrentValue[i] = false;
	}
	m_pInst = this;
}


void	CGameCenterManager::UpdateValue(int Type,bool Success,bool IsFinalDecision)
{
	// nothing to do
	if(IsAlradyGained(Type))
		return;
    
	// update here
	m_CurrentValue[Type] = Success;
    
	if(Success && IsFinalDecision)
	{
		// process here
		m_IsCompelete[Type] = true;
        
#ifdef _PLATFORM_IOS
        //CGameCenter_submitAchievement(MapTypeToName(Type).c_str(),
        //                              100,
         //                             OnSubmitCB,
         //                             OnErrorCB);
#endif
		
		
	}
}


std::string		CGameCenterManager::MapTypeToName(int Type)
{
    return "";
}

int             CGameCenterManager::MapNameToType(const std::string& TypeName)
{
	for (unsigned int i = 0; i < MAX_ACHIEVEMENT; i++)
		if (strcmpi( MapTypeToName(i).c_str(), TypeName.c_str() ) == 0)
			return i;
    
	return 0;  
}

bool	CGameCenterManager::GetValue(int Type)
{
	return m_CurrentValue[Type];
}

bool	CGameCenterManager::IsAlradyGained(int Type)
{
	return m_IsCompelete[Type];
}

int		CGameCenterManager::GetCompletedAchievementCount()
{
	int C = 0;
	for(int i = 0; (int)i < MAX_ACHIEVEMENT; i++)
	{
		if(m_IsCompelete[i])
			C++;
	}
	return C;
}

void	CGameCenterManager::Init()
{
	
#ifdef _PLATFORM_IOS
    CGameCenter_Init();
    if(CGameCenter_isGameCenterAvailable() == 1)
    {
        CGameCenter_authenticateLocalUser(LogInOnSuccessCB,LogInOnErrorCB);
    }
#endif
    
    
}

void	CGameCenterManager::ShutDown()
{
#ifdef _PLATFORM_IOS
    CGameCenter_DeInit();
#endif   
    
}

void		CGameCenterManager::ReportScore(int HighScore)
{
#ifdef _PLATFORM_IOS
    CGameCenter_reportScore(HighScore, GetLeaderBoardName().c_str());
#endif  
}


void	CGameCenterManager::ShowAchievements()
{
#ifdef _PLATFORM_IOS
    CGameCenter_ShowAchievements();
#endif  
    
}

void	CGameCenterManager::ShowLeaderBoard()
{
#ifdef _PLATFORM_IOS
    CGameCenter_ShowLeaderBoard(GetLeaderBoardName().c_str());
#endif  
	
}

void    OnSubmitCB(const char* ID)
{
    CGameCenterManager::Instance()->OnSuccessfulySubmitted(ID);
}


void    OnErrorCB(const char* ID)
{
    CGameCenterManager::Instance()->OnErrorSubmitted(ID);
}

void LogInOnSuccessCB(const wchar_t* PlayerName)
{
    CGameCenterManager::Instance()->OnLogInSuccess(PlayerName);
}

void LogInOnErrorCB()
{
    CGameCenterManager::Instance()->OnLogInError();
}

void    CGameCenterManager::OnGainAchievement(const char* ID)
{
    
}

void    CGameCenterManager::OnSuccessfulySubmitted(const char* ID)
{
    
}

void    CGameCenterManager::OnErrorSubmitted(const char* ID)
{
    
}

void    CGameCenterManager::OnLogInSuccess(const wchar_t* PlayerName)
{
    m_PlayerName = PlayerName;
    m_bIsLogInSuccessful = true;
	Internal_LoginOnSuccess(m_PlayerName);
}

void    CGameCenterManager::OnLogInError()
{
    m_bIsLogInSuccessful = false;
	Internal_LoginOnError();
}

std::string		CGameCenterManager::GetLeaderBoardName()
{
	return "";
}



